package com.shop.controller;


import com.shop.annotation.RequireRoles;
import com.shop.entity.Address;
import com.shop.model.vo.Rsp;
import com.shop.service.AddressService;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Delete;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * (Address)表控制层
 *
 * @author makejava
 * @since 2020-11-21 12:08:49
 */
@Validated
@RestController
@AllArgsConstructor
public class AddressController {
    /**
     * 服务对象
     */
    private final AddressService addressService;

    /**
     * 测试权限
     *
     * @param id
     * @return
     */
    @RequireRoles(roles = {"user", "merchant", "supplier"})
    @GetMapping("getDefaultAddressById")
    public Rsp getAddressById(@RequestHeader("userId") Integer id) {
        return addressService.getAddressById(id);
    }

    @RequireRoles(roles = {"user"})
    @GetMapping("getAllAddressById")
    public Rsp getAllAddressById(@RequestHeader("userId") Integer id) {
        return addressService.getAllAddressById(id);
    }

    /**
     * 测试请求头拿数据
     *
     * @param userId
     * @return
     */
    @RequireRoles(roles = {"user", "merchant", "supplier"})
    @GetMapping("getAddressFromHeadId")
    public Rsp getAddressFromHeadId(@RequestHeader("userId") Integer userId) {
        System.out.println(userId);
        return addressService.getAddressById(userId);
    }

    @PostMapping("addAddress")
    public Rsp addAddress(@RequestHeader("userId") Integer userId, @RequestBody Address address) {
        address.setUserId(userId);
        return addressService.addAddress(address);
    }

    @PutMapping("setDefaultAddress/{addrId}")
    public Rsp setDefaultAddress(@RequestHeader("userId") Integer userId, @PathVariable("addrId") Integer addrId) {
        return addressService.setDefaultAddress(userId, addrId);
    }

    @GetMapping("getAddress/{id}")
    public Rsp getAddress(@RequestHeader("userId") Integer userId, @PathVariable("id") Integer addrId) {
        return addressService.getAddress(userId, addrId);
    }

    @DeleteMapping("delAddress/{id}")
    public Rsp delAddress(@PathVariable("id") Integer addrId) {
        return addressService.delAddress(addrId);
    }
}
